//
//  ViewController.m
//  WEBVIEWAPP
//
//  Created by 赵国成 on 2018/4/16.
//  Copyright © 2018年 Guocheng. All rights reserved.
//

#import "RootViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import <WebKit/WebKit.h>
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define KIsiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)


@interface RootViewController ()<WKUIDelegate,WKNavigationDelegate>{
    
//    UIButton * backBtn;
    
}
@property(nonatomic,strong)WKWebView * webView;
//@property(nonatomic,assign)BOOL jump;
@end

@implementation RootViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.jump = NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.jump = NO;
//    self.view.backgroundColor = [UIColor greenColor];
     [self.view addSubview:self.webView];
    
   
    if (@available(iOS 11.0, *)) {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
       
    }else{
      
        self.automaticallyAdjustsScrollViewInsets = false;
        
    }
    
       if(KIsiPhoneX==YES){
            [self loadString:@"http://www.sharecdb.com/"];
       }else{
           [self loadString:@"http://www.sharecdb.com/"];
       }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NotHaveNetNotification:) name:@"NotHaveNet" object:nil];
    
    
    // 注册一个监听事件。第三个参数的事件名， 系统用这个参数来区别不同事件。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HaveNetNotification:) name:@"HaveNet" object:nil];
    
}
- (void)NotHaveNetNotification:(NSNotification *)noti
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:@"您已经断网，请检查网络权限" preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        NSLog(@"action = %@", action);
        
//        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
         [self loadString:@"http://www.sharecdb.com/"];
    }];
    
   
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)HaveNetNotification:(NSNotification *)noti
{
    
//    [_webView reload];

}

-(void)backBtnClick{
    
  
//    [_webView goToBackForwardListItem:_webView.backForwardList.backList[1]];
    
    
}
- (WKWebView *)webView   {
    if (!_webView) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        [config setValue:@YES forKey:@"_allowUniversalAccessFromFileURLs"];
             // 创建设置对象
             WKPreferences *preference = [[WKPreferences alloc]init];
             //最小字体大小 当将javaScriptEnabled属性设置为NO时，可以看到明显的效果
             preference.minimumFontSize = 0;
             //设置是否支持javaScript 默认是支持的
             preference.javaScriptEnabled = YES;
             // 在iOS上默认为NO，表示是否允许不经过用户交互由javaScript自动打开窗口
             preference.javaScriptCanOpenWindowsAutomatically = YES;
            
             config.preferences = preference;
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) configuration:config];
//        _webView.backgroundColor = [UIColor redColor];
//        _webView.allowsBackForwardNavigationGestures = YES;
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
           
    }
    return _webView;
    
}



// 在收到响应开始加载后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
    //返回支付宝的信息字符串，alipays:// 以后的为支付信息，这个信息后台是经过 URLEncode 后的，前端需要进行解码后才能跳转支付宝支付（坑点）

    WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
    //这句是必须加上的，不然会异常
    decisionHandler(actionPolicy);
}

- (void)loadString:(NSString *)str  {
  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"asdasd.html" ofType:nil];
    NSString *htmlString = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:htmlString]];
   
      [self.webView loadRequest:request];
    [self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
